<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>continuationtestcase_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_continuationtestcase_test.js.html">continuationtestcase_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.ContinuationTestCaseTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.ContinuationTestCaseTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.events&#39;);
<a name="line19"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.ContinuationTestCase&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.TestCase&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * @fileoverview This test file uses the ContinuationTestCase to test itself,
<a name="line28"></a> * which is a little confusing. It&#39;s also difficult to write a truly effective
<a name="line29"></a> * test, since testing a failure causes an actual failure in the test runner.
<a name="line30"></a> * All tests have been manually verified using a sophisticated combination of
<a name="line31"></a> * alerts and false assertions.
<a name="line32"></a> */
<a name="line33"></a>
<a name="line34"></a>var testCase = new goog.testing.ContinuationTestCase(&#39;Continuation Test Case&#39;);
<a name="line35"></a>testCase.autoDiscoverTests();
<a name="line36"></a>
<a name="line37"></a>// Standalone Closure Test Runner.
<a name="line38"></a>if (typeof G_testRunner != &#39;undefined&#39;) {
<a name="line39"></a>  G_testRunner.initialize(testCase);
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>var clock = new goog.testing.MockClock();
<a name="line44"></a>var count = 0;
<a name="line45"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>function setUpPage() {
<a name="line49"></a>  count = testCase.getCount();
<a name="line50"></a>}
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Resets the mock clock. Includes a wait step to verify that setUp routines
<a name="line55"></a> * can contain continuations.
<a name="line56"></a> */
<a name="line57"></a>function setUp() {
<a name="line58"></a>  waitForTimeout(function() {
<a name="line59"></a>    // Pointless assertion to verify that setUp methods can contain waits.
<a name="line60"></a>    assertEquals(count, testCase.getCount());
<a name="line61"></a>  }, 0);
<a name="line62"></a>
<a name="line63"></a>  clock.reset();
<a name="line64"></a>}
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Uninstalls the mock clock if it was installed, and restores the Step timeout
<a name="line69"></a> * functions to the default window implementations.
<a name="line70"></a> */
<a name="line71"></a>function tearDown() {
<a name="line72"></a>  clock.uninstall();
<a name="line73"></a>  stubs.reset();
<a name="line74"></a>
<a name="line75"></a>  waitForTimeout(function() {
<a name="line76"></a>    // Pointless assertion to verify that tearDown methods can contain waits.
<a name="line77"></a>    assertTrue(testCase.now() &gt;= testCase.startTime_);
<a name="line78"></a>  }, 0);
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Installs the Mock Clock and replaces the Step timeouts with the mock
<a name="line84"></a> * implementations.
<a name="line85"></a> */
<a name="line86"></a>function installMockClock() {
<a name="line87"></a>  clock.install();
<a name="line88"></a>
<a name="line89"></a>  // Overwrite the &quot;protected&quot; setTimeout and clearTimeout with the versions
<a name="line90"></a>  // replaced by MockClock. Normal tests should never do this, but we need to
<a name="line91"></a>  // test the ContinuationTest itself.
<a name="line92"></a>  stubs.set(goog.testing.ContinuationTestCase.Step, &#39;protectedClearTimeout_&#39;,
<a name="line93"></a>            window.clearTimeout);
<a name="line94"></a>  stubs.set(goog.testing.ContinuationTestCase.Step, &#39;protectedSetTimeout_&#39;,
<a name="line95"></a>            window.setTimeout);
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * @return {goog.testing.ContinuationTestCase.Step} A generic step in a
<a name="line101"></a> *     continuation test.
<a name="line102"></a> */
<a name="line103"></a>function getSampleStep() {
<a name="line104"></a>  return new goog.testing.ContinuationTestCase.Step(&#39;test&#39;, function() {});
<a name="line105"></a>}
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * @return {goog.testing.ContinuationTestCase.Test} A simple continuation test
<a name="line110"></a> *     with generic setUp, test, and tearDown functions.
<a name="line111"></a> */
<a name="line112"></a>function getSampleTest() {
<a name="line113"></a>  var setupStep = new goog.testing.TestCase.Test(&#39;setup&#39;, function() {});
<a name="line114"></a>  var testStep = new goog.testing.TestCase.Test(&#39;test&#39;, function() {});
<a name="line115"></a>  var teardownStep = new goog.testing.TestCase.Test(&#39;teardown&#39;, function() {});
<a name="line116"></a>
<a name="line117"></a>  return new goog.testing.ContinuationTestCase.Test(setupStep,
<a name="line118"></a>                                                    testStep,
<a name="line119"></a>                                                    teardownStep);
<a name="line120"></a>}
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>function testStepWaiting() {
<a name="line124"></a>  var step = getSampleStep();
<a name="line125"></a>  assertTrue(step.waiting);
<a name="line126"></a>}
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>function testStepSetTimeout() {
<a name="line130"></a>  installMockClock();
<a name="line131"></a>  var step = getSampleStep();
<a name="line132"></a>
<a name="line133"></a>  var timeoutReached = false;
<a name="line134"></a>  step.setTimeout(function() {timeoutReached = true}, 100);
<a name="line135"></a>
<a name="line136"></a>  clock.tick(50);
<a name="line137"></a>  assertFalse(timeoutReached);
<a name="line138"></a>  clock.tick(50);
<a name="line139"></a>  assertTrue(timeoutReached);
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>function testStepClearTimeout() {
<a name="line144"></a>  var step = new goog.testing.ContinuationTestCase.Step(&#39;test&#39;, function() {});
<a name="line145"></a>
<a name="line146"></a>  var timeoutReached = false;
<a name="line147"></a>  step.setTimeout(function() {timeoutReached = true}, 100);
<a name="line148"></a>
<a name="line149"></a>  clock.tick(50);
<a name="line150"></a>  assertFalse(timeoutReached);
<a name="line151"></a>  step.clearTimeout();
<a name="line152"></a>  clock.tick(50);
<a name="line153"></a>  assertFalse(timeoutReached);
<a name="line154"></a>}
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>function testTestPhases() {
<a name="line158"></a>  var test = getSampleTest();
<a name="line159"></a>
<a name="line160"></a>  assertEquals(&#39;setup&#39;, test.getCurrentPhase()[0].name);
<a name="line161"></a>  test.cancelCurrentPhase();
<a name="line162"></a>
<a name="line163"></a>  assertEquals(&#39;test&#39;, test.getCurrentPhase()[0].name);
<a name="line164"></a>  test.cancelCurrentPhase();
<a name="line165"></a>
<a name="line166"></a>  assertEquals(&#39;teardown&#39;, test.getCurrentPhase()[0].name);
<a name="line167"></a>  test.cancelCurrentPhase();
<a name="line168"></a>
<a name="line169"></a>  assertNull(test.getCurrentPhase());
<a name="line170"></a>}
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>function testTestSetError() {
<a name="line174"></a>  var test = getSampleTest();
<a name="line175"></a>
<a name="line176"></a>  var error1 = new Error(&#39;Oh noes!&#39;);
<a name="line177"></a>  var error2 = new Error(&#39;B0rken.&#39;);
<a name="line178"></a>
<a name="line179"></a>  assertNull(test.getError());
<a name="line180"></a>  test.setError(error1);
<a name="line181"></a>  assertEquals(error1, test.getError());
<a name="line182"></a>  test.setError(error2);
<a name="line183"></a>  assertEquals(&#39;Once an error has been set, it should not be overwritten.&#39;,
<a name="line184"></a>               error1, test.getError());
<a name="line185"></a>}
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>function testAddStep() {
<a name="line189"></a>  var test = getSampleTest();
<a name="line190"></a>  var step = getSampleStep();
<a name="line191"></a>
<a name="line192"></a>  // Try adding a step to each phase and then cancelling the phase.
<a name="line193"></a>  for (var i = 0; i &lt; 3; i++) {
<a name="line194"></a>    assertEquals(1, test.getCurrentPhase().length);
<a name="line195"></a>    test.addStep(step);
<a name="line196"></a>
<a name="line197"></a>    assertEquals(2, test.getCurrentPhase().length);
<a name="line198"></a>    assertEquals(step, test.getCurrentPhase()[1]);
<a name="line199"></a>    test.cancelCurrentPhase();
<a name="line200"></a>  }
<a name="line201"></a>
<a name="line202"></a>  assertNull(test.getCurrentPhase());
<a name="line203"></a>}
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>function testCancelTestPhase() {
<a name="line207"></a>  var test = getSampleTest();
<a name="line208"></a>
<a name="line209"></a>  test.cancelTestPhase();
<a name="line210"></a>  assertEquals(&#39;teardown&#39;, test.getCurrentPhase()[0].name);
<a name="line211"></a>
<a name="line212"></a>  test = getSampleTest();
<a name="line213"></a>  test.cancelCurrentPhase();
<a name="line214"></a>  test.cancelTestPhase();
<a name="line215"></a>  assertEquals(&#39;teardown&#39;, test.getCurrentPhase()[0].name);
<a name="line216"></a>
<a name="line217"></a>  test = getSampleTest();
<a name="line218"></a>  test.cancelTestPhase();
<a name="line219"></a>  test.cancelTestPhase();
<a name="line220"></a>  assertEquals(&#39;teardown&#39;, test.getCurrentPhase()[0].name);
<a name="line221"></a>}
<a name="line222"></a>
<a name="line223"></a>
<a name="line224"></a>function testWaitForTimeout() {
<a name="line225"></a>  var reachedA = false;
<a name="line226"></a>  var reachedB = false;
<a name="line227"></a>  var reachedC = false;
<a name="line228"></a>
<a name="line229"></a>  waitForTimeout(function a() {
<a name="line230"></a>    reachedA = true;
<a name="line231"></a>
<a name="line232"></a>    assertTrue(&#39;A must be true at callback a.&#39;, reachedA);
<a name="line233"></a>    assertFalse(&#39;B must be false at callback a.&#39;, reachedB);
<a name="line234"></a>    assertFalse(&#39;C must be false at callback a.&#39;, reachedC);
<a name="line235"></a>  }, 10);
<a name="line236"></a>
<a name="line237"></a>  waitForTimeout(function b() {
<a name="line238"></a>    reachedB = true;
<a name="line239"></a>
<a name="line240"></a>    assertTrue(&#39;A must be true at callback b.&#39;, reachedA);
<a name="line241"></a>    assertTrue(&#39;B must be true at callback b.&#39;, reachedB);
<a name="line242"></a>    assertFalse(&#39;C must be false at callback b.&#39;, reachedC);
<a name="line243"></a>  }, 20);
<a name="line244"></a>
<a name="line245"></a>  waitForTimeout(function c() {
<a name="line246"></a>    reachedC = true;
<a name="line247"></a>
<a name="line248"></a>    assertTrue(&#39;A must be true at callback c.&#39;, reachedA);
<a name="line249"></a>    assertTrue(&#39;B must be true at callback c.&#39;, reachedB);
<a name="line250"></a>    assertTrue(&#39;C must be true at callback c.&#39;, reachedC);
<a name="line251"></a>  }, 20);
<a name="line252"></a>
<a name="line253"></a>  assertFalse(&#39;a&#39;, reachedA);
<a name="line254"></a>  assertFalse(&#39;b&#39;, reachedB);
<a name="line255"></a>  assertFalse(&#39;c&#39;, reachedC);
<a name="line256"></a>}
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>function testWaitForEvent() {
<a name="line260"></a>  var et = new goog.events.EventTarget();
<a name="line261"></a>
<a name="line262"></a>  var eventFired = false;
<a name="line263"></a>  goog.events.listen(et, &#39;testPrefire&#39;, function() {
<a name="line264"></a>    eventFired = true;
<a name="line265"></a>    et.dispatchEvent(&#39;test&#39;);
<a name="line266"></a>  });
<a name="line267"></a>
<a name="line268"></a>  waitForEvent(et, &#39;test&#39;, function() {
<a name="line269"></a>    assertTrue(eventFired);
<a name="line270"></a>  });
<a name="line271"></a>
<a name="line272"></a>  et.dispatchEvent(&#39;testPrefire&#39;);
<a name="line273"></a>}
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>function testWaitForCondition() {
<a name="line277"></a>  var counter = 0;
<a name="line278"></a>
<a name="line279"></a>  waitForCondition(function() {
<a name="line280"></a>    return ++counter &gt;= 2;
<a name="line281"></a>  }, function() {
<a name="line282"></a>    assertEquals(2, counter);
<a name="line283"></a>  }, 10, 200);
<a name="line284"></a>}
<a name="line285"></a>
<a name="line286"></a>
<a name="line287"></a>function testOutOfOrderWaits() {
<a name="line288"></a>  var counter = 0;
<a name="line289"></a>
<a name="line290"></a>  // Note that if the delta between the timeout is too small, two
<a name="line291"></a>  // continuation may be invoked at the same timer tick, using the
<a name="line292"></a>  // registration order.
<a name="line293"></a>  waitForTimeout(function() {assertEquals(3, ++counter);}, 200);
<a name="line294"></a>  waitForTimeout(function() {assertEquals(1, ++counter);}, 0);
<a name="line295"></a>  waitForTimeout(function() {assertEquals(2, ++counter);}, 100);
<a name="line296"></a>}
<a name="line297"></a>
<a name="line298"></a>
<a name="line299"></a>/*
<a name="line300"></a> * Any of the test functions below (except the condition check passed into
<a name="line301"></a> * waitForCondition) can raise an assertion successfully. Any level of nested
<a name="line302"></a> * test steps should be possible, in any configuration.
<a name="line303"></a> */
<a name="line304"></a>
<a name="line305"></a>var testObj;
<a name="line306"></a>
<a name="line307"></a>
<a name="line308"></a>function testCrazyNestedWaitFunction() {
<a name="line309"></a>  testObj = {
<a name="line310"></a>    lock: true,
<a name="line311"></a>    et: new goog.events.EventTarget(),
<a name="line312"></a>    steps: 0
<a name="line313"></a>  };
<a name="line314"></a>
<a name="line315"></a>  waitForTimeout(handleTimeout, 10);
<a name="line316"></a>  waitForEvent(testObj.et, &#39;test&#39;, handleEvent);
<a name="line317"></a>  waitForCondition(condition, handleCondition, 1);
<a name="line318"></a>}
<a name="line319"></a>
<a name="line320"></a>function handleTimeout() {
<a name="line321"></a>  testObj.steps++;
<a name="line322"></a>  assertEquals(&#39;handleTimeout should be called first.&#39;, 1, testObj.steps);
<a name="line323"></a>  waitForTimeout(fireEvent, 10);
<a name="line324"></a>}
<a name="line325"></a>
<a name="line326"></a>function fireEvent() {
<a name="line327"></a>  testObj.steps++;
<a name="line328"></a>  assertEquals(&#39;fireEvent should be called second.&#39;, 2, testObj.steps);
<a name="line329"></a>  testObj.et.dispatchEvent(&#39;test&#39;);
<a name="line330"></a>}
<a name="line331"></a>
<a name="line332"></a>function handleEvent() {
<a name="line333"></a>  testObj.steps++;
<a name="line334"></a>  assertEquals(&#39;handleEvent should be called third.&#39;, 3, testObj.steps);
<a name="line335"></a>  testObj.lock = false;
<a name="line336"></a>}
<a name="line337"></a>
<a name="line338"></a>function condition() {
<a name="line339"></a>  return !testObj.lock;
<a name="line340"></a>}
<a name="line341"></a>
<a name="line342"></a>function handleCondition() {
<a name="line343"></a>  assertFalse(testObj.lock);
<a name="line344"></a>  testObj.steps++;
<a name="line345"></a>  assertEquals(&#39;handleCondition should be called last.&#39;, 4, testObj.steps);
<a name="line346"></a>}
</pre>


</body>
</html>
